package 字符串.第一个匹配项;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/1/17 16:31
 * @description:
 * @version:
 */
public class Solution {
    public static void main(String[] args) {
        String str = "mississippi";
        String target = "issipi";
        System.out.println(strStr(str, target));
    }

    public static int strStr(String str, String target) {
        if (str.length() < target.length()){
            return -1;
        }
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == target.charAt(0)){
                int j = i;
                int k = 0;
                while (k < target.length() && j < str.length() && str.charAt(j) == target.charAt(k)){
                    j++;
                    k++;
                    if (k == target.length()){
                        return j - target.length();
                    }
                }
            }
        }
        return -1;
    }
}
